package com.taru.houseapp.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taru.houseapp.commen.JsonResult;
import com.taru.houseapp.entity.Dynamic;
import com.taru.houseapp.entity.House;
import com.taru.houseapp.entity.HouseComment;
import com.taru.houseapp.entity.VoHouseColl;
import com.taru.houseapp.service.IHouseService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class HouseController {

    @Autowired
    IHouseService houseService;

    /**
     * 发布房源
     * @param house
     * @return
     */
    @RequestMapping(value = "/house/rent/out",method = RequestMethod.POST)
    public JsonResult insertHouse(@RequestBody House house){
        JsonResult result=null;
        try{
            String houseId=houseService.insertHouse(house);
            if(houseId !=null && houseId.length()>0){
                result=new JsonResult(200,"添加成功",houseId);
            }else {
                result=new JsonResult(404,"参数错误");
            }
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误",e.getMessage());
        }
        return  result;
    }
    /**
     * 删除房源图片路径
     * @param ids
     * @return
     */
    @RequestMapping(value = "/house/image",method = RequestMethod.DELETE)
    public JsonResult deleteHouseImage(@RequestBody List<Integer> ids){
        JsonResult result=null;
        if(ids==null || ids.size()<=0){
            result=new JsonResult(404,"参数错误");
            return result;
        }
        try{
            houseService.deleteHouseImage(ids);
            result=new JsonResult(200,"删除成功");
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误");
        }
        return result;
    }

    /**
     * 修改房源信息
     * @param house
     * @return
     */
    @RequestMapping(value = "/house",method = RequestMethod.PUT)
    public JsonResult updateHouse(@RequestBody House house){
        JsonResult result=null;
        try {
            int code=houseService.updateHouse(house);
            if(code>0){
                result=new JsonResult(200,"修改成功");
            }else {
                result=new JsonResult(404,"修改失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误",e.getMessage());
        }
        return result;
    }

    /**
     * 修改房源成为热门
     * @param house
     * @return
     */
    @RequestMapping(value = "/house/ishot",method = RequestMethod.PUT)
    public JsonResult updateHouseIsHot(@RequestBody House house){
        JsonResult result=null;
        if(house.getId()==null || house.getId().length()<=0 || house.getHotType()==null || house.getHotType().length()<=0 || "0".equals(house.getHotType())){
            result=new JsonResult(404,"参数错误");
            return result;
        }
        try {
            houseService.updateHouseIsHot(house);
            result=new JsonResult(200,"修改成功");
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误",e.getMessage());
        }
        return result;
    }

    /**
     * 修改出租状态
     * @param house
     * @return
     */
    @RequestMapping(value = "/house/rent/status",method = RequestMethod.PUT)
    public JsonResult updateHouseIsRent(@RequestBody House house){
        JsonResult result=null;
        if(house.getId()==null || house.getId().length()<=0 || house.getIsRent()==null || house.getIsRent().length()<=0){
            result=new JsonResult(404,"参数错误");
            return result;
        }
        try{
            houseService.updateHouseIsRent(house);
            result=new JsonResult(200,"修改成功");
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误",e.getMessage());
        }
        return result;
    }

    /**
     * 修改上架或下架
     * @param house
     * @return
     */
    @RequestMapping(value = "/house/shelf/status",method = RequestMethod.PUT)
    public JsonResult updateHouseIsShelf(@RequestBody House house){
        JsonResult result=null;
        if(house.getId()==null || house.getId().length()<=0 || house.getIsShelf()==null || house.getIsShelf().length()<=0){
            result=new JsonResult(404,"参数错误");
            return result;
        }
        try{
            houseService.updateHouseIsShelf(house);
            result=new JsonResult(200,"修改成功");
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误",e.getMessage());
        }
        return result;
    }

    /**
     * 查询房源的详情---用于出租者查看
     * @param id
     * @return
     */
    @RequestMapping(value = "/house/{id}",method = RequestMethod.GET)
   public JsonResult queryHouseById(@PathVariable("id") String id){
       JsonResult result=null;
        if(id ==null || id.length()<=0){
            result=new JsonResult(404,"参数错误");
        }
        try{
            House house=houseService.queryHouseById(id);
            result=new JsonResult(200,"获取成功",house);
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误",e.getMessage());
        }
        return result;
   }

    /**
     * 出租者查看自己房子
     * @param userId
     * @param code
     * @return
     */
    @RequestMapping(value = "/house/rentout",method = RequestMethod.GET)
    public JsonResult queryHouseByUserId( String userId, String code){
        JsonResult result=null;
        if(userId ==null || userId.length()<=0 || code ==null || code.length()<=0){
            result=new JsonResult(404,"参数错误");
        }
        try{
            List<House> houseList=houseService.queryHouseByUserId(userId,code);
            result=new JsonResult(200,"获取成功",houseList);
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误",e.getMessage());
        }
        return result;
    }

    /**
     * 查询房子详情(展示)
     * @return
     */
    @RequestMapping(value = "/house/all/{id}",method = RequestMethod.GET)
    public JsonResult queryHouseByIdRenter(@PathVariable("id") String id){
        JsonResult result=null;
        if(id ==null || id.length()<=0){
            result =new JsonResult(404,"参数错误");
            return result;
        }
        try{
            House house=houseService.queryHouseByIdRenter(id);
            result=new JsonResult(200,"获取成功",house);
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误",e.getMessage());
        }
        return result;
    }

    /**
     * 判断是否被当前用户收藏过
     * @param voHouseColl
     * @return
     */
    @RequestMapping(value = "/house/collection/judge",method = RequestMethod.POST)
    public JsonResult queryIsCollectionHouse(@RequestBody VoHouseColl voHouseColl){
        JsonResult result=null;
        if(voHouseColl.getHouseId()==null || voHouseColl.getHouseId().length()<=0 || voHouseColl.getUserId() ==null || voHouseColl.getUserId().length()<=0){
            result =new JsonResult(404,"参数错误");
            return result;
        }
        try{
            boolean isCollection=houseService.queryIsCollectionHouse(voHouseColl);
            result=new JsonResult(200,"获取成功",isCollection);
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误",e.getMessage());
        }
        return result;
    }

    /**
     * 添加收藏
     * @param voHouseColl
     * @return
     */
    @RequestMapping(value = "/house/collection",method = RequestMethod.POST)
    public JsonResult insertCollectionHouse(@RequestBody VoHouseColl voHouseColl){
        JsonResult result=null;
        if(voHouseColl.getHouseId()==null || voHouseColl.getHouseId().length()<=0 || voHouseColl.getUserId() ==null || voHouseColl.getUserId().length()<=0){
            result =new JsonResult(404,"参数错误");
            return result;
        }
        try{
            houseService.insertCollectionHouse(voHouseColl);
            result=new JsonResult(200,"添加成功");
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误",e.getMessage());
        }
        return result;
    }

    /**
     * 取消收藏
     * @param voHouseColl
     * @return
     */
    @RequestMapping(value = "/house/collection",method = RequestMethod.DELETE)
    public JsonResult deletedCollectionHouse(@RequestBody VoHouseColl voHouseColl){
        JsonResult result=null;
        if(voHouseColl.getHouseId()==null || voHouseColl.getHouseId().length()<=0 || voHouseColl.getUserId() ==null || voHouseColl.getUserId().length()<=0){
            result =new JsonResult(404,"参数错误");
            return result;
        }
        try{
            houseService.deletedCollectionHouse(voHouseColl);
            result=new JsonResult(200,"取消收藏成功");
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误",e.getMessage());
        }
        return result;
    }

    /**
     * 查看房子和房子出租人信息
     * @param houseId
     * @return
     */
    @RequestMapping(value = "/house/rentin",method = RequestMethod.GET)
    public JsonResult queryHouseAndRentOutUser(String houseId){
        JsonResult result=null;
        if(houseId==null || houseId.length()<=0){
            result =new JsonResult(404,"参数错误");
            return result;
        }
        try{
            House house=houseService.queryHouseAndRentOutUser(houseId);
            result=new JsonResult(200,"获取成功",house);
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误",e.getMessage());
        }
        return result;
    }

    /**
     * 租房人分类查询房源
     * @param city
     * @param villageName
     * @return
     */
    @RequestMapping(value = "/house/category",method = RequestMethod.GET)
    public JsonResult queryHouseByCategory(String city, String villageName,
                                           @RequestParam(value = "code",required = false,defaultValue = "") String code,
                                           @RequestParam(value = "orderNumber",required = false,defaultValue = "") String orderNumber,
                                           @RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,
                                           @RequestParam(value = "pageSize",required = false,defaultValue = "10") Integer pageSize){
        JsonResult result=null;
        if(city==null || villageName==null ||city.length()<=0 || villageName.length()<=0 ){
            result =new JsonResult(404,"参数错误");
            return result;
        }
        try{
            PageHelper.startPage(pageNum,pageSize);
            List<House> houseList=houseService.queryHouseByCategory(city,villageName,code,orderNumber);
            PageInfo pageInfo=new PageInfo(houseList);
            result=new JsonResult(200,"获取成功",pageInfo);
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误",e.getMessage());
        }
        return result;
    }

    /**
     * 查询收藏
     * @param userId
     * @param code
     * @return
     */
    @RequestMapping(value = "/collection",method = RequestMethod.GET)
    public JsonResult queryCollectionHouse(String userId,
                                           @RequestParam(value = "code",required = false,defaultValue = "") String code){
        JsonResult result=null;
        if(userId==null ||userId.length()<=0 ){
            result =new JsonResult(404,"参数错误");
            return result;
        }
        try{
            List<House> houseList=houseService.queryCollectionHouse(userId,code);
            result=new JsonResult(200,"获取成功",houseList);
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误",e.getMessage());
        }
        return result;
    }

    /**
     * 热门房子
     * @param city
     * @return
     */
    @RequestMapping(value = "/house/hot",method = RequestMethod.GET)
    public JsonResult queryHouseByHot(String city){
        JsonResult result=null;
        if(city==null ||city.length()<=0 ){
            result =new JsonResult(404,"参数错误");
            return result;
        }
        try{
            List<House> houseList=houseService.queryHouseByHot(city);
            result=new JsonResult(200,"获取成功",houseList);
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误",e.getMessage());
        }
        return result;
    }

    /**
     * 普通房源
     * @param city
     * @return
     */
    @RequestMapping(value = "/house/common",method = RequestMethod.GET)
    public JsonResult queryHouseByCommon(String city,
                                         @RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,
                                         @RequestParam(value = "pageSize",required = false,defaultValue = "10") Integer pageSize){
        JsonResult result=null;
        if(city==null ||city.length()<=0 ){
            result =new JsonResult(404,"参数错误");
            return result;
        }
        try{
            PageHelper.startPage(pageNum,pageSize);
            List<House> houseList=houseService.queryHouseByCommon(city);
            PageInfo pageInfo=new PageInfo(houseList);
            result=new JsonResult(200,"获取成功",pageInfo);
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误",e.getMessage());
        }
        return result;
    }
}
